﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SevenInvaders.common;

namespace SevenInvaders.LEVEL_DIEHL
{
    class Level_Diehl
    {
        DiehlPlayer diehlPlayer;
        //DiehlEnemy diehlEnemy;
        Texture2D diehlBackground;

        GameWindow Window;

        public Level_Diehl(ContentManager Content, GameWindow Window)
        {
            this.Window = Window;

            diehlPlayer = new DiehlPlayer(Content.Load<Texture2D>("LEVEL_DIEHL/megaman_nave"), new Vector2(0, 300), 1, Window, Content);
                       
            for (int i = 0; i < 5; i++)
            {
                DiehlEnemy.list.Add(new DiehlEnemy(Content.Load<Texture2D>("LEVEL_DIEHL/nave_enemy"), new Vector2(Window.ClientBounds.Width + ((Content.Load<Texture2D>("LEVEL_DIEHL/nave_enemy").Width+100)*i), Window.ClientBounds.Height - Content.Load<Texture2D>("LEVEL_DIEHL/nave_enemy").Height)));

               DiehlEnemy.list[i].currentIA = DiehlEnemy.IAType.goLeftUpAndDown;

            }

            DiehlEnemy.list[0].currentIA = DiehlEnemy.IAType.goLeft;
            DiehlEnemy.list[3].currentIA = DiehlEnemy.IAType.goLeft;


            diehlBackground = Content.Load<Texture2D>("LEVEL_DIEHL/diehlbackground");

        }
        public void Update(GameTime gameTime, KeyboardState ks, KeyboardState ks_anterior)
        {
            for (int i = 0; i < Shoot.list.Count; i++ )
            {
                Shoot.list[i].Update(gameTime);
            }

            diehlPlayer.Update(gameTime, ks, ks_anterior);

            for (int i = 0; i < Shoot.list.Count; i++)
            {
                Shoot.list[i].Update(gameTime);

                if (Shoot.list[i].PositionY < -Shoot.list[i].Texture.Height
                    || Shoot.list[i].PositionX > Window.ClientBounds.Width+Shoot.list[i].Texture.Width)
                {
                    Shoot.list[i].Visible = false;
                }

                for (int j = 0; j < DiehlEnemy.list.Count; j++)
                {
                    if (Shoot.list[i].Collision.Intersects(DiehlEnemy.list[j].Collision)
                        && Shoot.list[i].Visible && DiehlEnemy.list[j].Visible)
                    {
                        DiehlEnemy.list[j].Visible = false;
                        Shoot.list[i].Visible = false;
                    }
                }

            }

            for (int i = 0; i < DiehlEnemy.list.Count; i++)
            {
                DiehlEnemy.list[i].Update(gameTime);
            }

            for (int i = 0; i < DiehlEnemy.list.Count; i++)
            {
                if (!DiehlEnemy.list[i].Visible)
                {
                    DiehlEnemy.list.RemoveAt(i);
                }
            }

            for (int i = 0; i < Shoot.list.Count; i++)
            {
                if (!Shoot.list[i].Visible)
                {
                    Shoot.list.RemoveAt(i);
                }
            }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(diehlBackground, Vector2.Zero, Color.White);
            
            for (int i = 0; i < Shoot.list.Count; i++)
            {
                Shoot.list[i].Draw(gameTime, spriteBatch);
            }

            diehlPlayer.Draw(gameTime, spriteBatch);

            for (int i = 0; i < DiehlEnemy.list.Count; i++)
            {
                DiehlEnemy.list[i].Draw(gameTime, spriteBatch);
            }
            
        }
    }
}
